package com.itbuka.goods.controller;

import com.github.pagehelper.Page;
import com.itbuka.entity.PageResult;
import com.itbuka.entity.Result;
import com.itbuka.entity.StatusCode;
import com.itbuka.goods.domain.Spec;
import com.itbuka.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Spec")
@CrossOrigin
public class SpecController {
    @Autowired
    private SpecService iSpecService;

    /**
     * 查询全部
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<Spec>> selectAll(){
        return new Result<>("查询成功",iSpecService.selectAll());
    }
    /**
     * 插入数据
     * @param iSpec
     * @return
     */
    @PostMapping ("/insert")
    public Result<List<Spec>> insert(@RequestBody Spec iSpec){
        iSpecService.insert(iSpec);
        return new Result<>("新增成功");
    }
    /**
     * 更新数据
     * @param iSpec
     * @return
     */
    @PostMapping ("/update")
    public Result<List<Spec>> update(@RequestBody Spec iSpec){
        iSpecService.update(iSpec);
        return new Result<>("更新成功");
    }
    /**
     * 删除数据
     * @param ids
     * @return
     */
    @PostMapping ("/delete")
    public Result<List<Spec>> delete(@RequestParam String ids){
        iSpecService.delete(ids);
        return new Result<>("删除成功");
    }
    /**
     * 条件查询
     * @param iSpec
     * @return
     */
    @PostMapping("/selectList")
    public Result<List<Spec>> select(@RequestBody Spec iSpec){
        return new Result<>("条件查询成功", iSpecService.selectList(iSpec));
    }

    /**
     * 分页搜索实现
     * @param page 默认值1
     * @param size 默认值10
     * @return
     */
    public Result pageAll(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer size){
        Page<Spec> pageList = iSpecService.pageAll(page,size);
        PageResult pageResult = new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }
    /**
     * 分页条件搜索实现
     * @param iSpec
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestBody Spec iSpec,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Spec> pageList = iSpecService.pageList(iSpec,page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

}
